﻿using Game.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Game.DeviceHandler
{

    class ScriptDm : DeviceDm
    {

        public ScriptDm(int hWnd) : base(hWnd)
        {
        }

        private void TaskBoss()
        {

            //定位地图名称
            findMinMap();
            List<string> strList = new List<string>();
            strList.Add("1");
            this.dm.SetWindowState(this.hWnd, 12);
            this.dm.delay(1000);
            int k = 1;
            while (true)//isAbort 为真则暂停
            {
                Console.WriteLine("当前地图坐标{0}，{0}", m_minArr[2], m_minArr[3]);
                if (Common.Common.checkState)
                {
                    attack2(0);
                }
                else
                {
                    if (Common.Common.attackState <= 1 || Common.Common.attackState==3)
                    {
                        attack1(k, strList[0]);
                    }
                    else if (Common.Common.attackState == 2)
                    {
                        attack4(k);
                        
                    }
                    else if(Common.Common.attackState == 3)
                    {
                        
                    }
                }

                k++;
                Console.WriteLine("执行{0}次", k);
            }
        }

        private void attack2(int k, string tu)
        {
            int nRoleX, nRoleY;
            findRolePoint(out nRoleX, out nRoleY);
            if (nRoleX > 120)
            {
                moveAttack(37);
                MainDelay(500);
                moveJumpAttack(37, 39);
            }
            if (nRoleX < 30)
            {
                moveAttack(39);
                MainDelay(500);
                moveJumpAttack(39, 37);
            }
            for (var i = 0; i < 20; i++)
            {
                if (i < 10)
                {
                    if (i < 3)
                    {
                        moveAttack(37);
                    }
                    for (int j = 0; j < 5; i++)
                    {
                        if (findmaster() == 2)
                        {
                            //直接攻击
                        }
                        else if (findmaster() == 1)
                        {
                            //往右跳
                            moveAttack(39);

                        }
                        else if (findmaster() == 0)
                        {
                            moveAttack(37);
                        }
                    }
                }
                else
                {
                    if (i > 10)
                    {
                        if (i < 13)
                        {
                            moveAttack(37);
                        }
                        for (int j = 0; j < 5; i++)
                        {
                            if (findmaster() == 2)
                            {
                                //直接攻击
                            }
                            else if (findmaster() == 1)
                            {
                                //往右跳
                                moveAttack(39);

                            }
                            else if (findmaster() == 0)
                            {
                                moveAttack(37);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 攻击模式1
        /// </summary>
        private void attack1(int k, string tu)
        {

            int nRoleX, nRoleY;
            findRolePoint(out nRoleX, out nRoleY);
            if (Common.Common.attackState == 3)
            {
                if (k % 4 == 0)
                {
                    MainDelay(5000);
                    dm.KeyPress(49);
                    MainDelay(3000);
                }
            }
            if (nRoleX > 120)
            {
                moveAttack(37);
                MainDelay(500);
                moveJumpAttack(37, 39);
            }
            if (nRoleX < 30)
            {
                moveAttack(39);
                MainDelay(500);
                moveJumpAttack(39, 37);
            }
            if (k % 5 == 0)
            {

                //if (moveRole(132))
                //{
                //    for (int i = 0; i < 10; i++)
                //    {
                //        dm.KeyPress(65);
                //        MainDelay(Util.rnd(2500, 5500));
                //        dm.KeyPress(39);
                //        MainDelay(Util.rnd(2500, 4500));
                //        dm.KeyPress(37);
                //    }
                //    MainDelay(Util.rnd(60, 80) * 1000);
                //}
            }
           
            if (k % 1 == 0 && Common.Common.attackState == 0)
            {
                for (var i = 0; i < 15; i++)
                {
                    Console.WriteLine("当前坐标{0}i={1}", nRoleX, i);
                    findRolePoint(out nRoleX, out nRoleY);
                    if (nRoleX > Util.rnd(60, 85) && nRoleX < Util.rnd(85, 95))
                    {
                        for (int j = 0; j < 5; j++)
                        {
                            Console.WriteLine("tiao{0}", nRoleX);
                            MainDelay(500);
                            this.moveJump();
                            MainDelay(1000);
                            findRolePoint(out nRoleX, out nRoleY);
                           // if (nRoleY < 105)//85
                            if (nRoleY < 85)//85
                            {
                                moveAttack(39);//左边攻击
                                //i=0;
                                i = 15;
                                break;
                            }
                        }
                        MainDelay(500);
                        moveAttack(39);//左边攻击
                    }
                    if (i < 7)
                    {
                        if (nRoleX < 30)
                            continue;
                        moveAttack(37);//左边攻击
                       // MainDelay(500);
                       // moveJumpAttack(37, 39);
                        //attack3();
                        Console.WriteLine("往左攻击{0}", nRoleX);
                    }
                    else
                    {
                        if (nRoleX > 130)
                            continue;
                        moveAttack(39);//左边攻击
                        //MainDelay(500);
                        //moveJumpAttack(39, 37);
                        // attack3();
                        Console.WriteLine("往右攻击{0}", nRoleX);
                    }
                    //moveAttack(39);
                }
            }
            //MainDelay(500);
            //moveAttack(39);
            for (var i = 0; i < 30; i++)
            {
                findRolePoint(out nRoleX, out nRoleY);
                Console.WriteLine("1当前坐标{0}", nRoleX);
                if (i < 15)
                {
                    if (nRoleX < 20)
                        continue;
                    //MainDelay(500);
                    moveAttack(37);//左边攻击
                    //MainDelay(500);
                    //attack3();
                }

                else
                {
                    if (nRoleX > 130)
                        continue;
                    //MainDelay(500);
                    moveAttack(39);//左边攻击
                                   //MainDelay(500);
                                   // attack3();
                }
                if (i % 2 == 0)
                {
                    dm.delay(500);
                    dm.KeyPress(65);
                    dm.delay(Util.rnd(200, 400));
                    Console.WriteLine("攻击");
                }
                if (i % 3 == 0 && Common.Common.attackState == 3)
                {
                    MainDelay(1000);
                    dm.KeyPress(16);
                    MainDelay(1000);
                }

            }
        }

        private void attack3()
        {
            if (findmaster() == 2)
            {
                //直接攻击
                dm.KeyPress(65);
                MainDelay(500);
                dm.KeyPress(65);
                MainDelay(500);
                dm.KeyPress(65);
                Console.WriteLine("直接攻击");
            }
            else if (findmaster() == 1)
            {
                //往右跳
                moveAttack(39);
                Console.WriteLine("right");

            }
            else if (findmaster() == 0)
            {
                moveAttack(37);
                Console.WriteLine("left");
            }
            Console.WriteLine("攻击3");
        }

        private void attack4(int k)
        {
            if (k % 4 == 0)
            {
                MainDelay(1000);
                dm.KeyPress(16);
                MainDelay(100);
            }
            if (k % 80 == 0)
            {
                MainDelay(1000);
                dm.KeyPress(49);
                MainDelay(100);
            }
            int nRoleX, nRoleY;
            findRolePoint(out nRoleX, out nRoleY);
            for (int i = 0; i < 2; i++)
            {
                if (nRoleX < 30 )
                {
                    dm.KeyPress(39);
                    MainDelay(500);
                    dm.KeyPress(65);
          
                }
                if (nRoleX > m_minArr[2]-30 )
                {
                    dm.KeyPress(37);
                    MainDelay(500);
                    dm.KeyPress(65);
                    dm.KeyUp(37);
                }

            }
            Console.WriteLine("{0},{1}", nRoleX, m_minArr[2] - 20);
            MainDelay(100);
            dm.KeyPress(65);
            MainDelay(Util.rnd(300, 500));
            MainDelay(1000);
        }

        private void attack2(int k)
        {
            //string lujing = "114,100,0|113,85,1|60,85,1|60,70,0|142,100,0|40,100,0";
            //string lujing = "47,100,0|47,85,1|63,85,0|64,70,1|143,100,0";
           // string lujing = "100,97,0|99,79,1|25,97,0";//leve10C

            dm.SetDict(0, "zk.txt");
            dm.UseDict(0);
            string tu = "", lujing = "";

            for (int i = 0; i < 10; i++)
            {
                tu = dm.Ocr(4, 6, 230, 61, "ffffff", 0.98);
                lujing = "";
                if (tu != "")
                {
                    break;
                }
                dm.delay(100);
            }

            if (tu == "lev10A")
            {
                lujing = "114,100,0|113,85,1|65,85,1|65,70,0|142,100,0|40,100,0";
            }
            else if (tu == "lev10B")
            {
                lujing = "47,100,0|47,85,1|63,85,0|64,70,1|143,100,0";
            }
            else if (tu == "lev10C")
            {
                lujing = "100,97,0|99,79,1|25,97,0";//leve10C
            } else if (tu == "lev30A")
            {
                lujing = "139,97,0|139,85,1|71,96,0|72,81,1";//leve10C
               
             }
            else if (tu == "lev30B")
            {
                //lujing = "139,97,0|139,85,1|71,96,0|72,81,1";//leve10C
                lujing = " 144,97,0|144,81,1|256,60,0|90,81,1|28,97,0|28,85,1";
            }
            else if(tu == "lev30C")
            {
                lujing = "54,88,0|53,70,1|118,78,0|145,87,0";
            }
            Common.Common.str = lujing;
            string[] luArr = lujing.Split('|');
            while (true)
            {
                //记录首次y坐标

                int intY = 0, nRoleX, nRoleY;
                for (int i = 0; i < luArr.Length; i++)
                {
                    string[] mArr = luArr[i].Split(',');
                    int nX = int.Parse(mArr[0]); //当前结束点x坐标
                    if (i == 0)
                    {
                        intY = int.Parse(mArr[1]);
                    }
                    MainDelay(500);//查找用户坐标时先延迟
                    findRolePoint(out nRoleX, out nRoleY);
                    Common.Common.str = "当前x:"+nRoleX.ToString()+","+nRoleY.ToString();

                    if (nRoleY < intY-10 && i == 0) //首次进行攻击如果当前坐标不在最底层
                    {
                        //执行返回到最开始起点
                        for (int j = 0; j < 5; j++)
                        {
                            moveJump(37);
                            findRolePoint(out nRoleX, out nRoleY);
                            if (j == 4)
                            {
                                if (nRoleY != intY)
                                {
                                    j = 3;
                                }
                            }
                        }
                    }
                    //开始移动攻击
                    for (int a = 0; a < 15; a++)
                    {
                        MainDelay(500);
                        findRolePoint(out nRoleX, out nRoleY);
                        if (System.Math.Abs(nX - nRoleX) <= 5)
                        {
                            //走路到指定坐标

                            if (mArr[2] == "1")//移动指定位置 起跳
                            {

                                if (moveRole(nX,8))
                                {
                                    //进行跳动
                                    moveJump();
                                    //取下一个坐标y
                                   
                                    for (int s = 0; s < 4; s++)
                                    {
                                        MainDelay(200);
                                        findRolePoint(out nRoleX, out nRoleY);
                                        if (nRoleY<= int.Parse(mArr[1]))
                                        {
                                            MainDelay(200);
                                            dm.KeyPress(65);
                                            a = 15;
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (moveRole(nX))
                                {
                                    a = 15;
                                }
                            }
                        }
                        if (nX - nRoleX > 5)
                        {
                            moveAttack(39);
                        }
                        if (nX - nRoleX < -1)
                        {
                            if (nX - nRoleX < -5)
                            {
                                moveAttack(37);
                            }
                        }

                    }
                }
            }
        }

    }
}
